package com.ck.company.controller;

import com.ck.company.service.CpPictureGroupService;
import com.ck.constant.RequestConstant;
import com.ck.constant.ResponseConstant;
import com.ck.exception.ControllerException;
import com.ck.exception.ServiceException;
import com.ck.model.common.PictureGroup;
import com.ck.model.common.User;
import com.ck.model.common.sys.JsonResponse;
import com.ck.model.company.dto.ProgramPictureGroupTreeDTO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 图片组Controller
 *
 * @author dxy
 * @date 2018/7/3 18:03
 */
@Controller
@RequestMapping("/cpPictureGroup")
public class CpPictureGroupController {
	private static Logger logger = LoggerFactory.getLogger(CpPictureGroupController.class);
	@Autowired
	private CpPictureGroupService cpPictureGroupService;

	/**
	 * 保存图片组
	 */
	@ResponseBody
	@RequestMapping("/save")
	public Object savePictureGroup(PictureGroup pictureGroup, HttpServletRequest request) {
		try {
			//图片名称不能为空
			String pictureGroupName = pictureGroup.getPictureGroupName();
			if (StringUtils.isBlank(pictureGroupName)) {
				return new JsonResponse(ResponseConstant.CODE_FAIL, "图片组名称不能为空");
			}
			String picureGroupCode = pictureGroup.getPicureGroupCode();
			if (StringUtils.isBlank(picureGroupCode)) {
				return new JsonResponse(ResponseConstant.CODE_FAIL, "图片分组编码不能为空");
			}
			//验证图片分组名称和图片分组编码不能一样
			if (pictureGroupName.equals(picureGroupCode)) {
				return new JsonResponse(ResponseConstant.CODE_FAIL, "图片分组名称和图片分组编码不能一样");
			}
			User loginUser = (User) request.getSession().getAttribute(RequestConstant.SESSION_KEY_USER);
			String companyCode = loginUser.getCompanyCode();
			//验证图片名称是否存在
			PictureGroup namePictureGroup = cpPictureGroupService.getPictureGroupByName(companyCode, pictureGroupName);
			if (namePictureGroup != null) {
				return new JsonResponse(ResponseConstant.CODE_FAIL, "图片组名称已存在");
			}
			//验证图片编码是否存在
			PictureGroup codePictureGroup = cpPictureGroupService.getPictureGroupByCode(companyCode, picureGroupCode);
			if (codePictureGroup != null) {
				return new JsonResponse(ResponseConstant.CODE_FAIL, "图片组编码已存在");
			}
			cpPictureGroupService.savePictureGroup(pictureGroup, request);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("CpPictureGroupController savePictureGroup 保存图片组失败：" + e.getMessage());
			return new ControllerException("保存图片组失败： " + e.getMessage());
		}
		return "";
	}

	/**
	 * 修改图片组
	 */
	@ResponseBody
	@RequestMapping("/update")
	public Object updatePictureGroup(PictureGroup pictureGroup,String originalPicutreGroupName, String originalPicureGroupCode, HttpServletRequest request) {
		//图片名称不能为空
		String pictureGroupName = pictureGroup.getPictureGroupName();
		if (StringUtils.isBlank(pictureGroupName)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "图片组名称不能为空");
		}
		String picureGroupCode = pictureGroup.getPicureGroupCode();
		if (StringUtils.isBlank(picureGroupCode)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "图片分组编码不能为空");
		}
		//验证图片分组名称和图片分组编码不能一样
		if (pictureGroupName.equals(picureGroupCode)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "图片分组名称和图片分组编码不能一样");
		}
		try {
			User loginUser = (User) request.getSession().getAttribute(RequestConstant.SESSION_KEY_USER);
			String companyCode = loginUser.getCompanyCode();
			//如果新输入图片组名称与原始图片组名不一样，验证图片名称是否存在
			if (!pictureGroupName.equals(originalPicutreGroupName)) {
				PictureGroup namePictureGroup = cpPictureGroupService.getPictureGroupByName(companyCode, pictureGroupName);
				if (namePictureGroup != null) {
					return new JsonResponse(ResponseConstant.CODE_FAIL, "图片组名称已存在");
				}
			}
			//如果新输入图片组编码与原始图片组编码不一样，验证图片编码是否存在
			if (!picureGroupCode.equals(originalPicureGroupCode)) {
				PictureGroup codePictureGroup = cpPictureGroupService.getPictureGroupByCode(companyCode, picureGroupCode);
				if (codePictureGroup != null) {
					return new JsonResponse(ResponseConstant.CODE_FAIL, "图片组编码已存在");
				}
			}
			cpPictureGroupService.updatePictureGroup(pictureGroup, request);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("CpPictureGroupController updatePictureGroup 修改图片组失败：" + e.getMessage());
			return new ControllerException("修改图片组失败： " + e.getMessage());
		}
		return "";
	}

	/**
	 * 删除图片组
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public Object deletePictureGroup(Long pictureGroupId, HttpServletRequest request) {
		if (pictureGroupId == null) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "图片组ID不能为空");
		}
		try {
			cpPictureGroupService.deletePictureGroupById(pictureGroupId, request);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("CpPictureGroupController deletePictureGroup 删除图片组失败：" + e.getMessage());
			return e;
		}
		return "";
	}

	/**
	 * 跳转至选择图片组树
	 */
	@RequestMapping(value = "/goPictureGroupPicker")
	public String goPictureGroupPicker() {
		return "cp/cp_picturegroup_picker";
	}

	/**
	 * 获取公司的分组树列表
	 */
	@ResponseBody
	@RequestMapping(value = "/listPictureGroupTree")
	public Object listPictureGroupTree(HttpServletRequest request) {
		User loginUser = (User) request.getSession().getAttribute(RequestConstant.SESSION_KEY_USER);
		try {
			return cpPictureGroupService.listPictureGroupTree(loginUser.getCompanyCode());
		} catch (ServiceException e) {
			e.printStackTrace();
			logger.error("CpPictureGroupController listPictureGroupTree 获取公司的分组树列表失败：" + e.getMessage());
			return e;
		}
	}


	/**
	 * 获取公司的分组树列表(节目上传素材用)
	 */
	@ResponseBody
	@RequestMapping(value = "/listPictureGroupTreeForProgram")
	public Object listPictureGroupTreeForProgram(HttpServletRequest request, Long pictureGroupId) {
		User loginUser = (User) request.getSession().getAttribute(RequestConstant.SESSION_KEY_USER);
		try {
			List<ProgramPictureGroupTreeDTO> pictureGroupTreeDTOS = cpPictureGroupService.listProgramPictureGroupTree(
					loginUser.getCompanyCode(), pictureGroupId);
			return CollectionUtils.isNotEmpty(pictureGroupTreeDTOS) ? pictureGroupTreeDTOS : new ArrayList<>();
		} catch (ServiceException e) {
			e.printStackTrace();
			logger.error("CpPictureGroupController listPictureGroupTreeForProgram 获取公司的分组树列表(节目上传素材用)失败：" + e.getMessage());
			return new ArrayList<>();
		}
	}

	/**
	 * 获取公司的分组树列表(素材管理用)
	 */
	@ResponseBody
	@RequestMapping(value = "/treePictureGroupForProgram")
	public Object treePictureGroupForProgram(HttpServletRequest request, Long pictureGroupId) {
		User loginUser = (User) request.getSession().getAttribute(RequestConstant.SESSION_KEY_USER);
		try {
			List<ProgramPictureGroupTreeDTO> pictureGroupTreeDTOS = cpPictureGroupService.treePictureGroupForProgram(
					loginUser.getCompanyCode(), pictureGroupId);
			return CollectionUtils.isNotEmpty(pictureGroupTreeDTOS) ? pictureGroupTreeDTOS : new ArrayList<>();
		} catch (ServiceException e) {
			e.printStackTrace();
			logger.error("CpPictureGroupController listPictureGroupTreeForProgram 获取公司的分组树列表(节目上传素材用)失败：" + e.getMessage());
			return new ArrayList<>();
		}
	}

	/**
	 * 根据分组ID获取分组
	 */
	@ResponseBody
	@RequestMapping(value = "/getPictureGroupTreeForProgram")
	public Object getPictureGroupTreeForProgram(HttpServletRequest request, Long pictureGroupId) {
		User loginUser = (User) request.getSession().getAttribute(RequestConstant.SESSION_KEY_USER);
		try {
			ProgramPictureGroupTreeDTO pictureGroupTreeDTO = cpPictureGroupService.getPictureGroupTreeById(
					loginUser.getCompanyCode(), pictureGroupId);
			return pictureGroupTreeDTO;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("CpPictureGroupController getPictureGroupTreeForProgram 获取分组失败：" + e.getMessage());
			return null;
		}
	}
}
